<?php

class AddDongController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

    public $defaultAction = 'admin';
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

    function checkAdmin($user)
    {
        return $user->getState('login_name') != 'hanguyen';
    }

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
        return array(
            array('deny',  // deny all users
                    'actions'=>array('admin'),
                    'expression'    =>    array($this, 'checkAdmin'),
                ),

            array('deny',
                    'actions'=>array('admin'),
                    'users'    => array('?'),
            ),
        );
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
        if(isset($_POST['account'])){
            $coint = empty($_POST['amount'])?null:$_POST['amount'];
            $comment = empty($_POST['comment'])?null:$_POST['comment'];
            /**
            *
            * @var Account
            */
            $account = Account::model()->findByAttributes(array(
                'loginName'=>$_POST['account'],
            ));
            if(empty($account) || empty($coint))
                Yii::app()->user->setFlash('error', 'Tài khoản <b>'.$_POST['account'].'</b> không tồn tại');
            else{
                if($coint != KEY_ADD_DONG_DD) $this->addCointNormal($coint,$account,$comment);
                else $this->addCointForum($account);
            }
        }

        $model=new LogAddDong('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['LogAddDong']))
			$model->attributes=$_GET['LogAddDong'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

    public function addCointNormal($coint,$account,$comment=null)
    {
        $coint = intval($coint);
        $account->Coin = $account->Coin + $coint;
        $van = $coint/10000;
        $account->save(false);
        Yii::app()->user->setFlash('success', 'Add đồng cho tài khoản '.$account->loginName.' <b>'.$van.'</b> Vạn thành công');
        $logAdd = new LogAddDong();
        $logAdd->login_name = $account->loginName;
        $logAdd->amount = $coint;
        $logAdd->comment = $comment;
        $logAdd->save(false);
        $this->redirect($this->createUrl('addDong/admin'));
    }

    public function addCointForum($account)
    {
        $checkExist = LogAddDong::model()->findByAttributes(array(
            'login_name' => $account->loginName,
            'comment'=> KEY_ADD_DONG_DD,
        ));
        if(!empty($checkExist)){
            Yii::app()->user->setFlash('error', 'Đã thưởng add đồng trc đó');
            return;
        }
        $coint = 200000;
        $account->Coin = $account->Coin + $coint;
        $van = $coint/10000;
        $account->save(false);
        Yii::app()->user->setFlash('success', 'Add đồng cho tài khoản '.$account->loginName.' <b>'.$van.'</b> Vạn thành công');
        $logAdd = new LogAddDong();
        $logAdd->login_name = $account->loginName;
        $logAdd->amount = $coint;
        $logAdd->comment = KEY_ADD_DONG_DD;
        $logAdd->save(false);
        $this->redirect($this->createUrl('addDong/admin'));
    }

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=LogAddDong::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='log-add-dong-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
